package com.ajie.ft_kvdata;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.ajie.ft_kvdata.sp.SpLancher;

import java.util.Iterator;
import java.util.Map;

/**
 * TODO
 *  结论：可用，去掉了一些 校验。
 *  创建文件夹失败 /bbox/media
 *  加权限 有可创建文件夹 android:sharedUserId="android.uid.system"，
 */
public class CustomSpMainActivity extends AppCompatActivity {
    private static final String TAG = "CustomSpMainActivity";
    private final String SP_DIR = "/bbox/media";
    private SharedPreferences sp;
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_sp_main);

        sp = new SpLancher().getSharedPreferences("kvdata2",SP_DIR, Context.MODE_PRIVATE);
    }

    int count = 0;
    public void putSp( View view) {
        SharedPreferences.Editor editor = sp.edit().putString((count++) + "key",   count + " value");
        editor.commit();
    }


    public void spForEach(View view) {
        Map<String, ?> map = sp.getAll();
        Iterator<? extends Map.Entry<String, ?>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry<String, ?> entry = iterator.next();
            Log.i(TAG, "spForEach: k:"+entry.getKey()+"  v:"+entry.getValue());
        }

    }
}